#!/bin/bash

RUNONCE="0";

# test if input contains clean using glob:
if [[ "$@" == *clean* ]]; then
  RUNONCE="1";
  echo "Cleaning..."
  rm -rf build
  mkdir build
  echo "Done Cleaning"
fi

# test if input contains make using glob:
if [[ "$@" == *make* ]]; then
  RUNONCE="1";
  echo "Making..."
  # make build dir if it doesnt exist:
  if [ ! -d "build" ]; then
    mkdir build
  fi
  # move to build dir:
  cd build
  # generate make file:
  cmake ..
  #test if cmake worked: test most recent return value $?
  if [ $? -ne 0 ]; then
    echo "CMAKE FAILED"
    exit 1
  fi
  #cmake worked so make it:
  echo "building..."
  make
  if [ $? -ne 0 ]; then
    echo "MAKE FAILED"
    exit 1
  fi
  # move back up in case of "run"
  cd ..
  echo "Done Making"
fi

# test if input contains run using glob:
if [[ "$@" == *run* ]]; then
  RUNONCE="1";
  echo "Running..."
  build/main
  echo "Terminated"
fi

if [ "$RUNONCE" = "0" ]; then
  echo "expected: ./compile [clean] [make] [run]"
fi


